package jmine.tec.web.wicket.component.report;

import java.util.ArrayList;
import java.util.List;

import jmine.tec.report.api.ReportHeader;
import jmine.tec.report.api.table.ReportCell;
import jmine.tec.utils.Tuple;
import jmine.tec.web.wicket.component.InlinedLabel;
import jmine.tec.web.wicket.component.InlinedPanels;

import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.Model;

/**
 * {@link Panel} que renderiza os parâmetros do header
 * 
 * @author bruno.ozahata
 * @created Apr 13, 2012
 */
public class HeaderContentPanel extends Panel {

    private final ReportHeader header;

    /**
     * C'tor
     * 
     * @param id id
     * @param header the {@link ReportHeader}
     */
    public HeaderContentPanel(String id, ReportHeader header) {
        super(id);
        this.header = header;
        this.generateComponents();
    }

    /**
     * Cria os componentes
     */
    private void generateComponents() {
        this.add(new GridView<ReportCell>("rows", new ListDataProvider<ReportCell>(this.header.getParameters())) {

            @Override
            protected void populateEmptyItem(Item<ReportCell> item) {
                item.add(new EmptyPanel("id"));
            }

            @Override
            protected void populateItem(Item<ReportCell> item) {
                ReportCell cell = item.getModelObject();
                Tuple<?, ?> param = (Tuple<?, ?>) cell.getValue();
                List<Panel> panels = new ArrayList<Panel>();
                panels.add(new InlinedLabel("cell", String.format("%s: ", param.getHead())).addToLabel(new AttributeAppender("style",
                        new Model<String>("font-weight: bold;"), " ")));
                panels.add(new InlinedLabel("cell", param.getTail().toString()));
                item.add(new InlinedPanels("id", panels));
            }
        }.setColumns(this.header.getParametersPerRow()));

    }

}
